home *** CD-ROM | disk | FTP | other *** search
- { AccMenu.pas -- Add accelerator keys to menu commands }
-
- program AccMenu;
-
- {$R accmenu.res}
-
- uses WinTypes, WinProcs, WObjects;
-
- const
-
- id_Menu = 100; { Menu resource ID }
- id_Accs = 200; { Accelerator resource ID }
- cm_About = 101; { About command ID }
- cm_Help = 102; { Help command ID }
- cm_Quit = 103; { Exit command ID }
-
- type
-
- AccMenuApplication = object(TApplication)
- procedure InitInstance; virtual;
- procedure InitMainWindow; virtual;
- end;
-
- PAccMenuWindow = ^AccMenuWindow;
- AccMenuWindow = object(TWindow)
- CurrentHotKey: Word;
- constructor Init(AParent: PWindowsObject; ATitle: PChar);
- procedure CMAbout(var Msg: TMessage);
- procedure CMHelp(var Msg: TMessage);
- virtual cm_First + cm_Help;
- procedure WMCommand(var Msg: TMessage);
- virtual wm_First + wm_Command;
- procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
- virtual;
- end;
-
-
- { AccMenuApplication }
-
- {- Initialize this instance of the application }
- procedure AccMenuApplication.InitInstance;
- begin
- TApplication.InitInstance;
- HAccTable := LoadAccelerators(HInstance, PChar(id_Accs))
- end;
-
- {- Initialize AccMenuApplication object's window }
- procedure AccMenuApplication.InitMainWindow;
- begin
- MainWindow := New(PAccMenuWindow, Init(nil, 'Accelerated Menu'))
- end;
-
-
- { AccMenuWindow }
-
- {- Construct AccWindow object }
- constructor AccMenuWindow.Init(AParent: PWindowsObject; ATitle: PChar);
- begin
- TWindow.Init(AParent, ATitle);
- Attr.Menu := LoadMenu(HInstance, PChar(id_Menu));
- CurrentHotKey := 0 { none }
- end;
-
- {- Execute the About command. Called by WMCommand }
- procedure AccMenuWindow.CMAbout(var Msg: TMessage);
- begin
- MessageBox(HWindow, 'Accelerator Demonstration', 'About', mb_Ok)
- end;
-
- {- Execute the Help command }
- procedure AccMenuWindow.CMHelp(var Msg: TMessage);
- begin
- MessageBox(HWindow, 'Press F1...F9', 'Help!', mb_Ok)
- end;
-
- {- Intercept wm_Command messages }
- procedure AccMenuWindow.WMCommand(var Msg: TMessage);
- begin
- case Msg.WParam of
- cm_About: CMAbout(Msg);
- cm_Quit: CloseWindow;
- vk_F1 .. vk_F9:
- begin
- CurrentHotKey := Msg.WParam;
- InvalidateRect(HWindow, nil, true)
- end
- else
- TWindow.WMCommand(Msg)
- end
- end;
-
- {- Display current hot key (if any) }
- procedure AccMenuWindow.Paint(PaintDC: HDC;
- var PaintInfo: TPaintStruct);
- var
- HotKeyStr: string[5];
- S: string[40];
- begin
- TWindow.Paint(PaintDC, PaintInfo);
- if CurrentHotKey = 0 then
- HotKeyStr := 'x'
- else
- Str(CurrentHotKey - vk_F1 + 1, HotKeyStr);
- S := 'Current hot key = F' + HotKeyStr;
- TextOut(PaintDC, 120, 60, @S[1], Length(S))
- end;
-
- var
-
- AccMenuApp: AccMenuApplication;
-
- begin
- AccMenuApp.Init('AccMenuApp');
- AccMenuApp.Run;
- AccMenuApp.Done
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 2/22/1991
- ---------------------------------------------------------------}
-